using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Lzfy_His_Service.Models;
using Lzfy_His_Service.Services;
using System.ComponentModel.DataAnnotations;

namespace Lzfy_His_Service.Controllers
{
    /// <summary>
    /// 账户操作日志管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class AccountOpLogController : ControllerBase
    {
        private readonly IAccountOpLogService _accountOpLogService;
        private readonly ILogger<AccountOpLogController> _logger;

        public AccountOpLogController(IAccountOpLogService accountOpLogService, ILogger<AccountOpLogController> logger)
        {
            _accountOpLogService = accountOpLogService;
            _logger = logger;
        }

        /// <summary>
        /// 创建账户操作日志
        /// </summary>
        /// <param name="request">创建请求</param>
        /// <returns>创建结果</returns>
        [HttpPost("create")]
        public async Task<ActionResult<AccountOpLogResponse>> Create([FromBody] AccountOpLogCreateRequest request)
        {
            try
            {
                if (request == null)
                {
                    return BadRequest("请求不能为空");
                }

                var response = await _accountOpLogService.CreateAsync(request);
                
                if (response.Success)
                {
                    return Ok(response);
                }
                else
                {
                    return BadRequest(response);
                }
            }
            catch (ArgumentNullException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建账户操作日志时发生错误");
                return StatusCode(500, "内部服务器错误");
            }
        }
    }
}